package com.example.csupermaket.controller;


import com.example.csupermaket.pojo.dto.BannerAddNewDTO;
import com.example.csupermaket.pojo.dto.BannerUpdateDTO;
import com.example.csupermaket.pojo.vo.BannerStandardVO;
import com.example.csupermaket.service.IBannerService;
import com.example.csupermaket.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@RestController
@RequestMapping("/banners")
@Validated
@Api(tags = "05.轮播图管理模块")
public class BannerController {


    @Autowired
    private IBannerService bannerService;


    @PostMapping("/add-new")
    @ApiOperation("01.添加轮播图")
    @ApiOperationSupport(order = 100)
    private JsonResult insert(BannerAddNewDTO bannerAddNewDTO){

        bannerService.insert(bannerAddNewDTO);

        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("02.删除轮播图")
    @ApiOperationSupport(order = 200)
    private JsonResult deleteById(int bid){
        String url = bannerService.selectUrlById(bid);
//        new File(dirPath+url).delete();
        bannerService.deleteById(bid);

        return JsonResult.ok();
    }

    @PostMapping("/{bid:[0-9]+}/update")
    @ApiOperation("03.修改轮播图")
    @ApiOperationSupport(order = 300)
    public JsonResult update(int bid, BannerUpdateDTO bannerUpdateDTO){
        bannerService.update(bid,bannerUpdateDTO);
        return JsonResult.ok();
    }

    @RequestMapping("/{bid:[0-9]+}/selecturl")
    public JsonResult selectUrlById(int bid){
        String queryResult = bannerService.selectUrlById(bid);

        return JsonResult.ok(queryResult);
    }

    @RequestMapping("/{bid:[0-9]+}")
    public JsonResult selectById(int bid){

        BannerStandardVO queryResult = bannerService.selectById(bid);

        return JsonResult.ok(queryResult);
    }

}
